package com.kingschan.util.encryption;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

/**
 * des ����
 * 
 * <pre>
 * Des d = new Des();
 * String a = d.getEncriptCode(d.getKey(&quot;key&quot;.getBytes()), &quot;����һ������&quot;);
 * System.out.println(&quot;���ܺ�&quot; + a);
 * String b = d.getDecriptCode(a, d.getKey(&quot;key&quot;.getBytes()));
 * System.out.println(&quot;���ܺ�:&quot; + b);
 * </pre>
 * 
 * @author kingschan
 * 
 */
public class Des {

	/**
	 * �Զ���һ��key,���8λ�ַ�Ľ�Ϊ8λ
	 */
	public Key getKey(byte[] keyByte) {
		Key key = null;
		byte[] byteTemp = new byte[8];// ����һ���յİ�λ����,Ĭ�������Ϊ0
		for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {// ���û�ָ���Ĺ���ת���ɰ�λ����
			byteTemp[i] = keyByte[i];
		}
		key = new SecretKeySpec(byteTemp, "DES");
		return key;
	}

	/**
	 * �ڶ��ֲ���key�ķ���
	 * 
	 * @return
	 */
	public Key getKey2() {
		Key key = null;
		SecureRandom sr = new SecureRandom();// ����һ�������ε������Դ��DES�㷨��Ҫ
		try {
			KeyGenerator kg = KeyGenerator.getInstance("DES");// ��DES�㷨����һ��KeyGenerator����
			kg.init(sr);// ��ʼ������Կ�����,ʹ�����ȷ������Կ����
			key = kg.generateKey(); // ����ܳ�
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return key;
	}

	/**
	 * @param key
	 *            ����ʹ�õ���Կ��
	 * @return ������ĵ��ַ��ʾ��ʽ
	 */
	public String getEncriptCode(Key key, String srcCode) {
		StringBuffer sb = null;

		try {
			/*
			 * Cipher���޹��췽��������getInstance()������������ת������ƴ��ݸ��� ����Ϊ ת������ƣ�����
			 * DES/CBC/PKCS5Padding����������ʹ��DESת����
			 */
			Cipher encriptCipher = Cipher.getInstance("DES");
			encriptCipher.init(Cipher.ENCRYPT_MODE, key);// ����Կ��ʼ���� Cipher
			byte[] desCode = encriptCipher.doFinal(srcCode.getBytes());// �������ֲ����������
			sb = new StringBuffer(desCode.length * 2);// �����ܺ�����ת����16���Ƶ��ַ���
			for (int i = 0; i < desCode.length; i++) {
				int temp = desCode[i];
				if (temp < 0) {// �Ѹ���ת��Ϊ����
					temp = temp + 256;// byte����СֵΪ-256,���ֵΪ255
				}
				// С�� 0F ������Ҫ��ǰ�油0
				if (temp < 16) {
					sb.append("0");
				}
				sb.append(Integer.toString(temp, 16));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	// �����㷨
	public String getDecriptCode(String encriptCode, Key key) {
		Cipher decriptCipher = null;
		String decriptString = null;
		byte[] encriptByte = encriptCode.getBytes();

		byte[] decriptByte = new byte[encriptByte.length / 2];
		for (int i = 0; i < encriptByte.length; i += 2) {
			String strTmp = new String(encriptByte, i, 2);
			decriptByte[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}

		try {
			decriptCipher = Cipher.getInstance("DES");
			decriptCipher.init(Cipher.DECRYPT_MODE, key);

			byte[] outByte = decriptCipher.doFinal(decriptByte);
			decriptString = new String(outByte);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decriptString;
	}

}
